<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2018/11/9
 * Time: 上午10:52
 */

namespace app\service;
use app\common\model\GoodsSku;
use app\common\model\GoodsSkuValue;
use app\common\model\Goods as GoodsModel;
use app\model\GoodsTypeModel;
use think\Exception;

/**
 * 商品
 * Class GoodsService
 * @package app\service
 */
class GoodsService extends BaseService
{
    protected $goodsModel;
    protected $goodsTypeModel;
    public function __construct()
    {
        $this->goodsTypeModel=new GoodsTypeModel();//M('goodstype');
        $this->goodsModel=new GoodsModel();//M('');
    }

    /**根据商品ID查询出商品信息
     * @param $goods_id
     * @param null $filed
     * @return array|bool
     */
    public function findGoodsInfoByGoodsId($goods_id,$filed=null){
        try{
            $where['id']=$goods_id;
            if(empty($filed)){
                return $this->goodsModel->where($where)->find();
            }else{
                return $this->goodsModel->field($filed)->where($where)->find();
            }
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }

    }

    /**
     * 查询总商品数量
     * @param $where
     * @return bool|int|string
     */
    public function countGoods($where){
        try{
            return $this->goodsModel->where($where)->count();
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }
    }

    /**
     * 修改订单信息
     * @param $data
     * @param $where
     * @return false|int
     */
    public function editGoods($data,$where){
        return $this->goodsModel->save($data,$where);
    }

    /**
     * 商品字段
     * @param $id
     * @param $filed
     * @param $num
     * @param string $type
     * @return bool|int|true
     */
    public function incGoodsFiled($id,$filed,$num,$type='inc'){
        try{
            if($type=='inc'){
                return $this->goodsModel->where('id',$id)->setInc($filed,$num);
            }else{
                return $this->goodsModel->where('id',$id)->setDec($filed,$num);
            }

        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }
    }

    //处理商品
    public static function dealGoods($goods){
        foreach ($goods as $k=>$v){
            $goods[$k]['picture']=tomedia($v['picture']);
            if(isset($v['picture_list']) && !empty($v['picture_list'])){
                $pic_list=json_decode($v['picture_list'],true);
                foreach ($pic_list as $pk=>$pv){
                    $pic_list[$pk]['path']=tomedia($pv['path']);
                }
                $goods[$k]['picture_list']=$pic_list;
            }
            $goods[$k]['sku'] = self::skuDetail($v['id']);
        }
        return $goods;
        try{
            
        }catch (Exception $ex){
            return false;
        }

    }


    /**
     * 根据条件查询出商品信息
     * @param $where
     * @param null $filed
     * @return array|bool
     */
    public function findGoodsInfoByWhere($where,$filed=null){
        try{
            if(empty($filed)){
                return $this->goodsModel
                    ->field($filed)
                    ->where($where)
                    ->find()->toArray();
            }else{
                return $this->goodsModel->field($filed)->where($where)->find()->toArray();
            }
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }
    }

    //默认自查询字段
    public function goodsFiled(){
        return '
        id,
        goods_name,
        shop_price,
        market_price,
        stock,is_new,
        is_hot,
        is_shiwu,
        stock,
        freight_payer,
        gtype_id,
        goods_pic,
        goods_thumb,
        weight,
        goods_desc,
        grade_id,
        sale_all,
        shipping';
    }

    /**
     * 根据类型查询出商品信息
     * @param $type_id
     * @param null $filed
     * @return array|bool
     */
    public function queryGoodsInfoByTypeId($type_id,$filed=null){
        try{
            $where['gtype_id']=$type_id;
            if(empty($filed)){
                return $this->goodsModel->where($where)->select()->toArray();
            }else{
                return $this->goodsModel->field($filed)->where($where)->select()->toArray();
            }
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }

    }

    /**
     * 根据类型查询出商品信息
     * @param $where
     * @param null $filed
     * @param string $ord
     * @return array|bool
     */
    public function queryGoodsInfoByWhere($where,$filed=null,$ord='ord'){
        try{
            if(empty($filed)){
                return $this->goodsModel->where($where)->order($ord,'desc')->select()->toArray();
            }else{
                return $this->goodsModel->field($filed)->where($where)->order($ord,'desc')->select()->toArray();
            }
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }

    }

    /**根据类型查询出商品信息
     * @param $where
     * @param int $page
     * @param int $page_num
     * @param null $filed
     * @param string $ord
     * @return array|bool
     */
    public function queryGoodsInfoByWherePage($where,$page=1,$page_num=20,$filed=null,$ord='ord'){
        try{
            if(empty($filed)){
                return $this->goodsModel->where($where)->page($page,$page_num)->order($ord,'desc')->select()->toArray();
            }else{
                return $this->goodsModel->field($filed)->where($where)->page($page,$page_num)->order($ord,'desc')->select()->toArray();
            }
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }

    }

    /**
     * 查询商品列表
     * @param $where
     * @param $page
     * @param int $page_num
     * @param null $filed
     * @param string $order_by
     * @return array|bool
     */
    public function queryGoodsListByWhere($where,$page,$page_num=10,$filed=null,$order_by='ord ASC'){
        try{
            if(empty($filed)){
                return $this->goodsModel->where($where)->order($order_by)->page($page,$page_num)->select()->toArray();
            }else{
                return $this->goodsModel->field($filed)->where($where)->order($order_by)->page($page,$page_num)->select()->toArray();
            }
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }

    }

    /**
     * @return array|bool
     */
    public function backGoodsTypeInfo(){
        try{
            $goodsType=$this->goodsTypeModel->select();
            $back=array();
            foreach ($goodsType as $k=>$v){
                $back[$v['gtype_id']]=$v['gtype_name'];
            }
            return $back;
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }

    }

    /**修改购物车数量
     * @param $uid
     * @param $goods_id
     * @param $goods_num
     * @return bool|false|int
     */
    public function saveCart($uid,$goods_id,$goods_num){
        try{
            $where['uid']=$uid;
            $where['goods_id']=$goods_id;
            $save['goods_num']=$goods_num;
            return $this->goodsModel->where($where)->save($save);
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }

    }

    /* 获取商品sku信息 */
    public static function skuDetail ($goods_id) {
        $goodsInfo = GoodsModel::getOneById($goods_id);
        $sku = [];
        if($goodsInfo['s1']) {
            $sku[] = [
                'key' => 's1',
                'name' => $goodsInfo['s1'],
                'list' => GoodsSkuValue::getAll([
                    'goods_id' => $goodsInfo['id'],
                    'status' => 1,
                    'type' => 's1'
                ],'id asc','*'),
            ];
        }
        if($goodsInfo['s2']) {
            $sku[] = [
                'key' => 's2',
                'name' => $goodsInfo['s2'],
                'list' => GoodsSkuValue::getAll([
                    'goods_id' => $goodsInfo['id'],
                    'status' => 1,
                    'type' => 's2'
                ],'id asc','*'),
            ];
        }
        $list = GoodsSku::getAll([
            'goods_id' => $goodsInfo['id'],
            'status' => 1
        ]);
        return [
            'tree' => $sku,
            'list' => $list?$list:[],
        ];
    }

}